package com.mzy.interview

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午3:22
 * @Description:
 */
class 两两交换链表中的节点 {
    fun swapPairs(head: ListNode?): ListNode? {
        if(head == null) return null
        var dummy : ListNode? = ListNode(-1, head)
        val ans  = dummy

        while (dummy?.next != null && dummy.next?.next != null){
            val next = dummy.next
            val nextTwo = dummy.next?.next
            dummy.next = nextTwo
            next?.next = nextTwo?.next
            nextTwo?.next = next
            dummy = next
        }

        return ans?.next
    }
}

fun main() {
    val lit = ListNode(1, ListNode(2, ListNode(3, ListNode(4,ListNode(5)))))
    两两交换链表中的节点().swapPairs(lit)
}